define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'community/project/publicwelfare/index' + location.search,
                    add_url: 'community/project/publicwelfare/add',
                    edit_url: 'community/project/publicwelfare/edit',
                    del_url: 'community/project/publicwelfare/del',
                    multi_url: 'community/project/publicwelfare/multi',
                    import_url: 'community/project/publicwelfare/import',
                    table: 'community_publicwelfare',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rememberOrder: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'school_name', title: __('School_name'), operate: 'LIKE', sortable: true, searchable: true},
                        {field: 'community_name', title: '社区名称', operate: 'LIKE', sortable: true, searchable: true},
                        {field: 'project_name', title: __('Project_name'), operate: 'LIKE', sortable: true, searchable: true},
                        {field: 'cover', title: __('Cover'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                            {
                                name: 'course',
                                text: '课程代码配置',
                                icon: 'fa fa-book',
                                classname: 'btn btn-xs btn-info btn-dialog',
                                url: 'community/config/course/index?community_id={community_id}&publicwelfare_id={id}'
                            },
                            {
                                name: 'community',
                                text: '社区代码配置',
                                icon: 'fa fa-home',
                                classname: 'btn btn-xs btn-warning btn-dialog',
                                url: 'community/config/community/index?community_id={community_id}&publicwelfare_id={id}'
                            },
                            {
                                name: 'timezone',
                                text: '时区代码配置',
                                icon: 'fa fa-clock-o',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: 'community/config/timezone/index?community_id={community_id}&publicwelfare_id={id}'
                            },
                            {
                                name: 'timeslot',
                                text: '时间关联配置',
                                icon: 'fa fa-calendar',
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                url: 'community/config/timeslot/index?community_id={community_id}&publicwelfare_id={id}'
                            }
                        ]}
                    ]
                ]
            });

            // 初始化搜索表单
            var searchForm = $(".form-commonsearch");
            Form.api.bindevent(searchForm);
            
            // 添加快捷搜索功能
            $(document).on("click", ".btn-quick-search", function () {
                var value = $("input[name='search']").val();
                table.bootstrapTable('refresh', {query: {search: value}});
            });
            $(document).on("keyup", "input[name='search']", function (event) {
                if (event.keyCode === 13) {
                    $(".btn-quick-search").trigger("click");
                    event.preventDefault();
                }
            });
            
            // 引入通用的城市和学校过滤脚本
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/index.js';
            document.head.appendChild(script);
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});